package com.example.propertymanagement.controller;

import com.example.propertymanagement.dto.BuildingDTO;
import com.example.propertymanagement.entity.Building;
import com.example.propertymanagement.entity.Community;
import com.example.propertymanagement.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * BuildingController：用于处理与楼栋相关的 HTTP 请求
 */
@RestController
@RequestMapping("/api/building") // 设置请求路径前缀
public class BuildingController {

    @Autowired
    private BuildingService buildingService; // 注入 BuildingService

    /**
     * 获取所有楼栋信息
     * @return 返回楼栋信息的列表
     */
    @GetMapping("/list") // 设置 GET 请求路径
    public List<BuildingDTO> getBuildingList() {
        return buildingService.getAllBuildings(); // 调用 Service 层获取楼栋信息
    }

    /**
     * 根据小区ID获取楼栋信息
     * @param communityId 小区ID
     * @return 返回楼栋DTO列表
     */
    @GetMapping("/community/{communityId}")
    public List<BuildingDTO> getBuildingsByCommunity(@PathVariable Integer communityId) {
        return buildingService.getBuildingsByCommunity(communityId); // 调用Service层根据小区ID获取楼栋
    }

    /**
     * 根据楼栋ID获取楼栋信息
     * @param id 楼栋ID
     * @return 返回楼栋DTO
     */
    @GetMapping("/{id}")
    public BuildingDTO getBuildingById(@PathVariable Integer id) {
        return buildingService.getBuildingById(id)
                .orElseThrow(() -> new RuntimeException("楼栋未找到"));
    }

    /**
     * 创建新楼栋
     * @param buildingDTO 新的楼栋信息
     * @return 返回创建的楼栋信息
     */
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)  // 设置响应状态为创建成功
    public BuildingDTO createBuilding(@RequestBody BuildingDTO buildingDTO) {
        Building building = new Building();
        building.setBuildingCode(buildingDTO.getBuildingCode());
        building.setBuildingName(buildingDTO.getBuildingName());
        building.setTotalHouseholds(buildingDTO.getTotalHouseholds());
        building.setDescription(buildingDTO.getDescription());
        building.setCreateTime(buildingDTO.getCreateTime());
        building.setOperator(buildingDTO.getOperator());
        building.setCommunity(new Community());  // 设置关联的小区ID
        return buildingService.createBuilding(building);  // 返回创建后的楼栋信息
    }

    /**
     * 更新楼栋信息
     * @param id 楼栋ID
     * @param buildingDTO 更新后的楼栋信息
     * @return 返回更新后的楼栋信息
     */
    @PutMapping("/{id}")
    public BuildingDTO updateBuilding(@PathVariable Integer id, @RequestBody BuildingDTO buildingDTO) {
        buildingDTO.setId(id);  // 设置更新的ID
        Building building = new Building();
        building.setId(buildingDTO.getId());
        building.setBuildingCode(buildingDTO.getBuildingCode());
        building.setBuildingName(buildingDTO.getBuildingName());
        building.setTotalHouseholds(buildingDTO.getTotalHouseholds());
        building.setDescription(buildingDTO.getDescription());
        building.setCreateTime(buildingDTO.getCreateTime());
        building.setOperator(buildingDTO.getOperator());
        building.setCommunity(new Community());  // 设置关联的小区ID
        return buildingService.updateBuilding(building);  // 返回更新后的楼栋信息
    }

    /**
     * 删除楼栋信息
     * @param id 楼栋ID
     */
    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)  // 设置响应状态为无内容
    public void deleteBuilding(@PathVariable Integer id) {
        buildingService.deleteBuilding(id);  // 调用Service层删除楼栋
    }
}